using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using PiscoSourDominio;
using System.Collections.Generic;
using Dapper;
using System;

namespace PiscoSourSOAPServices.Persistencia
{

    public class PlatoDAO
    {
        public string InsertarPlato(Plato plato)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "INSERT INTO PSR_PLATO(intCodPlato,vchNombre,vchDescripcion,decPrecio,blnEstado) VALUES (@intCodPlato,@vchNombre,@vchDescripcion,@decPrecio,@blnEstado)";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodPlato = plato.intCodPlato,
                            vchNombre = plato.vchNombre,
                            vchDescripcion = plato.vchDescripcion,
                            decPrecio = plato.decPrecio,
                            blnEstado = plato.blnEstado
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ActualizarPlato(Plato plato)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {
                    sqlConnection.Open();
                    string sqlQuery = "UPDATE PSR_PLATO SET vchNombre=@vchNombre,vchDescripcion=@vchDescripcion,decPrecio=@decPrecio,blnEstado=@blnEstado WHERE intCodPlato=@intCodPlato";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodPlato = plato.intCodPlato,
                            vchNombre = plato.vchNombre,
                            vchDescripcion = plato.vchDescripcion,
                            decPrecio = plato.decPrecio,
                            blnEstado = plato.blnEstado
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string EliminarPlato(Plato plato)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "DELETE FROM PSR_PLATO WHERE intCodPlato=@intCodPlato";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intCodPlato = plato.intCodPlato
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<Plato> ListarPlatos()
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                var sqlQuery = string.Format(@"Select * from PSR_PLATO");
                var result = sqlConnection.Query<Plato>(sqlQuery);
                sqlConnection.Close();
                return result;
            }
        }
    }
}